<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once INTE_ROOT . 'IPost.php';
require_once DEF_ROOT . 'PostDef.php';
require_once DATA_ROOT . 'PostData.php';
require_once DATA_ROOT . 'ReplyPostData.php';
require_once UTIL_ROOT . 'Preprocessor.php';

class BasePost {

    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new BasePost();
        }
        return self::$instance;
    }

    protected static $instance = null;

    protected function __construct() {
        
    }

    /*
     *
     */

    public function getPostByPid($pid) {
        $ret = array('err' => ErrorDef::OK);
        try {
            $arr_cond = array('pid' => $pid);
            $arr_ne_cond = array('status' => PostStatusDef::DELETED);
            $res = PostData::model()->getData(null, $arr_cond, $arr_ne_cond);
            if (ErrorDef::OK == $res['err']) {
                if (!empty($res['data'])) {
                    $post = $res['data'][0];
                    $post->vt_content = Preprocessor::getContent($post->type, $post->vt_content);
                    $ret['data'] = $post;
                } else {
                    $ret['err'] = ErrorDef::COMMON_ERROR_NOT_EXIST;
                }
            }
        } catch (Exception $e) {
            $err_code = $e->getCode();
            if (empty($err_code)) {
                $ret['err'] = ErrorDef::COMMON_ERROR_SYS;
            } else {
                $ret['err'] = $err_code;
            }
        }
        return $ret;
    }

    /*
     *
     */

    public function getPostListByUid($uid, $type = null) {
        $ret = array('err' => ErrorDef::OK);
        try {
            $arr_cond = array('uid' => $uid);
            if (!is_null($type)) {
                $arr_cond['type'] = $type;
            }
            $arr_ne_cond = array('status' => PostStatusDef::DELETED);
            $res = PostData::model()->getData(null, $arr_cond, $arr_ne_cond);
            if (ErrorDef::OK == $res['err'] && empty($res['data'])) {
                $ret['err'] = ErrorDef::COMMON_ERROR_NOT_EXIST;
            } else {
                $posts = array();
                foreach ($res as $post) {                    
                    $post->vt_content = Preprocessor::getContent($post->type, $post->vt_content);
                    $posts[] = $post;
                }
                $ret['data'] = $posts;
            }
        } catch (Exception $e) {
            $err_code = $e->getCode();
            if (empty($err_code)) {
                $ret['err'] = ErrorDef::COMMON_ERROR_SYS;
            } else {
                $ret['err'] = $err_code;
            }
        }

        return $ret;
    }

    /*
     *
     */

    public function getPostListByGid($gid, $type = null) {
        $ret = array('err' => ErrorDef::OK);
        try {
            $arr_cond = array('gid' => $gid);
            if (!is_null($type)) {
                $arr_cond['type'] = $type;
            }
            $arr_ne_cond = array('status' => PostStatusDef::DELETED);
            $res = PostData::model()->getData(null, $arr_cond, $arr_ne_cond);
            if (ErrorDef::OK == $res['err'] && empty($res['data'])) {
                $ret['err'] = ErrorDef::COMMON_ERROR_NOT_EXIST;
            } else {
                $posts = array();
                foreach ($res as $post) {                 
                    $post->vt_content = Preprocessor::getContent($post->type, $post->vt_content);
                    $posts[] = $post;
                }
                $ret['data'] = $posts;
            }
        } catch (Exception $e) {
            $err_code = $e->getCode();
            if (empty($err_code)) {
                $ret['err'] = ErrorDef::COMMON_ERROR_SYS;
            } else {
                $ret['err'] = $err_code;
            }
        }

        return $ret;
    }

    /*
     *
     */

    public function getReplyList($pid, $pnum, $psize) {
        $ret = array('err' => ErrorDef::OK, 'data' => array());
        try {
            $data = array();
            $conds = array('pid' => $pid, 'status' => ReplyStatusDef::NORMAL);
            $res = ReplyPostData::model()->findData(null, $conds, null, null, 'create_time', false, $pnum * $psize, $psize, $count);
            $replys = array();
            foreach ($res as $reply) {
                $reply->vt_content = Preprocessor::getContent(PostTypeDef::TEXT, $reply->vt_content);
                $replys[] = $reply;
            }

            $ret['data'] = $replys;
            $ret['tn'] = $count;
            $ret['pn'] = $pnum;
            $ret['ps'] = count($replys);
        } catch (Exception $e) {
            $err_code = $e->getCode();
            if (empty($err_code)) {
                $ret['err'] = ErrorDef::COMMON_ERROR_SYS;
            } else {
                $ret['err'] = $err_code;
            }
        }
        return $ret;
    }

    /*
     *
     */

    public function addPost($title, $content, $type, $uid, $gid = 0, $category_id = 0, $config = null) {
        throw new Exception('Can not invoke function[' . __CLASS__ . '::' . __FUNCTION__ . '], this is a dummy base function', ErrorDef::COMMON_ERROR_INVOKE);
    }

    /*
     *
     */

    public function editPost($pid, $title, $content, $config = null) {
        $ret = array('err' => ErrorDef::OK, 'data' => array());
        try {
            if (empty($pid) || empty($content))
            {
                throw new Exception('the pid for the post to be edit can not be empty', ErrorDef::COMMON_ERROR_PARAM);
            }
            $arr_cond = array('pid' => $pid);
            $arr_ne_cond = array('status' => PostStatusDef::DELETED);
            $res = PostData::model()->findData(null, $arr_cond, $arr_ne_cond);
            $postData = null;
            if (ErrorDef::OK == $res['err']) {
                if (!empty($res['data'])) {
                    $postData = $res['data'][0];                    
                } else {
                    throw new Exception('the data for pid[' . $pid . '] is not exist', ErrorDef::COMMON_ERROR_NOT_EXIST);
                }
            }
            $newContent = Preprocessor::editContent($postData->type, $postData->vt_content, $content);
            $postData->editPost($newContent, $title, $config);
        }catch (Exception $e)
        {
            $err_code = $e->getCode();
            if (empty($err_code)) {
                $ret['err'] = ErrorDef::COMMON_ERROR_SYS;
            } else {
                $ret['err'] = $err_code;
            }
        }
        
        return $ret;
    }

    /*
     *
     */

    public function removePost($pid) {
        
    }

    /*
     *
     */

    public function forwardPost($pid) {
        
    }

    /*
     *
     */

    public function replyPost($replyData) {
        
    }

}